﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mc_tester
{
    // helyben tárolja a portra kiküldendő adatokat, de a port állapotok nem tükrözik a MC állapotát
    public class Port
    {
        //a jobb olvashatóság miatt
        public enum DIRECTIONS { IN, OUT };
        public enum LEVELS { HIGH, LOW };

        public enum COMMANDS { GET, ADC, TIMERSTART, TIMERSTOP };

        private string _name="";

        //ez a port byte azonosítója
        private byte _byteValue;

        private DIRECTIONS _direction;
        private LEVELS _level;

        private bool _adcMode;

        private int _idoparam=1;
        private int _ciklikusidoparam=0;

        private bool _timeractive=false;

        private PortControl _portControl;
        private Device _device;

        //konstructorok
        public Port(){

        }
        public Port(Device d,string Name, byte b, bool adc){
            this._device = d;
            this._byteValue = b;
            this._name = Name;
            _level = LEVELS.LOW;
            _direction = DIRECTIONS.OUT;
            _adcMode = adc;
            // a portnak kell UserControl
            _portControl = new PortControl(this,d);   
        }

        public PortControl portControl
        {
            get
            {
                return _portControl;
            }
        }
        public bool adcMode
        {
            get
            {
                return _adcMode;
            }
        }
        public byte byteValue
        {
            get
            {
                return _byteValue;
            }
        }
        public string name
        {
            get
            {
                return _name;
            }
        }
        public DIRECTIONS portDirection
        {
            get
            {
                return _direction;
            }
            set
            {
                _direction = value;
            }
        }

        public LEVELS portLevel
        {
            get
            {
                return _level;
            }
            set
            {
                _level = value;
                _portControl.setLed(value);
            }
        }

        public bool timeractive
        {
            get
            {
                return _timeractive;
            }
            set
            {
                _timeractive = value;
            }
        }

        public int idoparam
        {
            get
            {
                return _idoparam;
            }
            set
            {
                _idoparam = value;
            }
        }
        public int ciklikusidoparam
        {
            get
            {
                return _ciklikusidoparam;
            }
            set
            {
                _ciklikusidoparam = value;
            }
        }

        //tovább passzoljuk az üzenetküldést a device-nak
        public void sendMessage(string s)
        {
            _device.sendMessage(s);
        }

        public void starttimer(int ido, int cikl){
            _idoparam = ido;
            _ciklikusidoparam = cikl;
            _timeractive = true;
            _device.activetimer++;
            _portControl.settimerbuttoncolor(System.Drawing.Color.Red);
            sendMessage(Port.COMMANDS.TIMERSTART.ToString() + " " + name+ " "+ ido.ToString() + " " + cikl.ToString());
        }

        public void stoptimer(int ido, int cikl)
        {
            _idoparam = ido;
            _ciklikusidoparam = cikl;
            _timeractive = false;
            _device.activetimer--;
            _portControl.settimerbuttoncolor(System.Drawing.Color.Black);
            sendMessage(Port.COMMANDS.TIMERSTOP.ToString() + " " + name);
        }

    }// end Port class
}
